package org.autocode.component;

import javafx.beans.property.ObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebErrorEvent;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import org.autocode.util.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @program: autocode
 * @description:
 * @author: zhu.xiaojie
 * @create: 2020-08-17 22:08
 */
public class CodeView extends AnchorPane implements Initializable {

    @FXML
    private WebView webView;
    private WebEngine engine;


    public CodeView() {
        FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("fxml/CodeView.fxml"));
        loader.setController(this);
        loader.setRoot(this);

        try {
            loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setText(String language , String text){
        setLanguage(language);

        Node node = this.engine.getDocument().getElementsByTagName("code").item(0);
        node.setTextContent(text);

        this.engine.executeScript("Prism.highlightAll();");
    }

    private void setLanguage(String language){
        Node node = this.engine.getDocument().getElementsByTagName("code").item(0);
        NamedNodeMap attributes = node.getAttributes();
        Node aClass = attributes.getNamedItem("class");
        aClass.setTextContent("language-" + language);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.engine = this.webView.getEngine();
        this.engine.setJavaScriptEnabled(true);
        String html = loadContent("html/code.html");


        Document document = Jsoup.parse(html);
        Element head = document.getElementsByTag("head").get(0);

        String style = loadContent("html/prism.css");
        head.append("<style>\n"+ style +"\n</style>");

        String js = loadContent("html/prism.js");
        head.append("<script>\n"+ js +"\n</script>");
        html = document.html();

        this.engine.loadContent(html);
//        this.engine.load("https://prismjs.com/");

        engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
            @Override
            public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {

            }
        });



    }

    private String loadContent(String path){
        try(InputStream in = getClass().getClassLoader().getResourceAsStream(path)){

            String s = IOUtils.toString(in);
            return s;

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
